﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents;
using System.ComponentModel;
using ProgNetComponents.Design.Editors;
using System.Drawing.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using ProgNetComponents.Design.Attributes;
using DevExpress.XtraGrid.Views.Grid;
using ProgNetComponents.ExtenderProviders;
using System.Data;
using DevExpress.Utils;
using System.ComponentModel.Design.Serialization;

namespace ProgNetDxComponents.ExtenderProviders.Settings
{
    public class ModeSettings : ProgNetComponents.ExtenderProviders.Settings.BaseExtenderSettings
    {
        #region ctor
        public ModeSettings(object owner, ModeProvider provider) : base(owner, provider) {
            AllowNewRecordModes = FormMode.None;
        }
        #endregion

        #region Members
        private FormMode _ReadOnlyModes = FormMode.Read | FormMode.View;
        private FormMode _RequiredModes;
        private FormMode _AllowNewRecordModes = FormMode.Edit | FormMode.New;
        private FormMode _AllowDeleteRecordModes = FormMode.Edit | FormMode.New;
        #endregion
         
        #region Properties
        [Editor(typeof(FlagEnumEditor), typeof(UITypeEditor)),
        DynamicHiddenByType("Owner", typeof(GridView)),
        RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public FormMode ReadOnlyModes
        {
            get { return _ReadOnlyModes; }
            set
            {
                if (_ReadOnlyModes != value)
                {
                    _ReadOnlyModes = value;
                    OnSettingsChanged();
                }
            }
        }

        [Editor(typeof(FlagEnumEditor), typeof(UITypeEditor)),
        DynamicBrowsableByType("Owner", typeof(BaseEdit), typeof(GridColumn)),
        RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public FormMode RequiredModes
        {
            get { return _RequiredModes; }
            set
            {
                if (_RequiredModes != value)
                {
                    _RequiredModes = value;
                    OnSettingsChanged();
                }
            }
        }

        [DynamicBrowsableByType("Owner", typeof(GridColumn)),
        RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public DefaultBoolean RowAsModeSetter
        {
            get;
            set;
        }

        #region GridView
        [
            Editor(typeof(FlagEnumEditor), typeof(UITypeEditor)),
            DynamicBrowsableByType("Owner", typeof(GridView)),
            RefreshProperties(System.ComponentModel.RefreshProperties.All)
        ]
        public FormMode AllowNewRecordModes
        {
            get { return _AllowNewRecordModes; }
            set
            {
                if (_AllowNewRecordModes != value)
                {
                    _AllowNewRecordModes = value;
                    OnSettingsChanged();
                }
            }
        }

        [
            Editor(typeof(FlagEnumEditor), typeof(UITypeEditor)),
            DynamicBrowsableByType("Owner", typeof(GridView)),
            RefreshProperties(System.ComponentModel.RefreshProperties.All)
        ]
        public FormMode AllowDeleteRecordModes
        {
            get { return _AllowDeleteRecordModes; }
            set
            {
                if (_AllowDeleteRecordModes != value)
                {
                    _AllowDeleteRecordModes = value;
                    OnSettingsChanged();
                }
            }
        }
        
        [
            DynamicHiddenByProperty("AllowNewRecordModes", FormMode.None)
        ]
        public bool NewRowAtTop
        {
            get;
            set;
        }

        [
           DynamicBrowsableByType("Owner", typeof(GridView))
        ]
        public DefaultBoolean ParentRowAsModeSetter
        {
            get;
            set;
        }
        #endregion
        #endregion
       
        #region events methods
        public void OnCustomGetGridColumnModes(object sender, ModeSettingsForColumnEventArgs e)
        {
            if (CustomGetGridColumnModes != null)
            {
                CustomGetGridColumnModes(sender, e);
            }
        }
        public void OnCustomGetControlModes(object sender, ModeSettingsEventArgs e)
        {
            if (CustomGetControlModes != null)
            {
                CustomGetControlModes(sender, e);
            }
        }
        public void OnCustomGridNewRowEnabled(object sender, AllowNewRowEventArgs e)
        {
            if (CustomGridNewRowEnabled != null)
            {
                CustomGridNewRowEnabled(sender, e);
            }
        }
        public void OnRowDeleting(object sender, RowDeletingEventArgs e)
        {
            if (RowDeleting != null)
                RowDeleting(sender, e);
        }
        #endregion
        
        #region Events
        [DynamicBrowsableByType("Owner", typeof(GridColumn))]
        public event EventHandler<ModeSettingsForColumnEventArgs> CustomGetGridColumnModes;

        [DynamicHiddenByType("Owner", typeof(GridColumn), typeof(GridView))]
        public event EventHandler<ModeSettingsEventArgs> CustomGetControlModes;

        [DynamicBrowsableByType("Owner", typeof(GridView))]
        public event EventHandler<AllowNewRowEventArgs> CustomGridNewRowEnabled;

        [DynamicBrowsableByType("Owner", typeof(GridView))]
        public event EventHandler<RowDeletingEventArgs> RowDeleting;

        #endregion
    }
    public class RowDeletingEventArgs : CancelEventArgs
    {
        public RowDeletingEventArgs(DataRow row)
        {
            _Row = row;
        }

        private DataRow _Row;
        public DataRow Row
        {
            get { return _Row; }
        }
    }
    public class AllowNewRowEventArgs : EventArgs
    {
        private int _RowHandle, _RelationIndex;
        public int RowHandle
        {
            get
            {
                return _RowHandle;
            }
        }
        public int RelationIndex
        {
            get
            {
                return _RelationIndex;
            }
        }
        public AllowNewRowEventArgs(int rowHandle, int relationIndex)
        {
            _RowHandle = rowHandle;
            _RelationIndex = relationIndex;
        }
        public bool AllowNewRow
        {
            get;
            set;
        }
    }
    public class ModeSettingsEventArgs : EventArgs
    {
        public bool IsReadOnly
        {
            get;
            set;
        }
        public bool IsRequired
        {
            get;
            set;
        }
    }
    public class ModeSettingsForColumnEventArgs : ModeSettingsEventArgs
    {
        private int _RowHandle;
        private GridColumn _Column;
        public int RowHandle
        {
            get
            {
                return _RowHandle;
            }
        }
        public GridColumn Column
        {
            get { return _Column; }
        }
        public ModeSettingsForColumnEventArgs(GridColumn column, int rowHandle)
        {
            _RowHandle = rowHandle;
            _Column = column;
        }

    }

}
